#! -*- coding:utf-8 -*-
import logging
from datetime import datetime, timedelta

from django.core.management.base import BaseCommand
from common.cache import redis_cache
from common.bank_record.model import BankRecord, RECORD_STATUS
from common.order.db import add_order_success
from common.mch import handler as mch_handler

_LOGGER = logging.getLogger('worker')

# cd /home/ubuntu/af-env/hydra;/home/ubuntu/af-env/bin/python manage.py

class Command(BaseCommand):

    def handle(self, the_day=0, **options):
        pass
        # print 'start'
        # _LOGGER.info('update_order_status start')
        # start = datetime.now() + timedelta(hours=(-8 - int(the_day) * 24))
        # items = BankRecord.query.filter(BankRecord.created_at >= start).filter(
        #     BankRecord.status == RECORD_STATUS.UNMATCH).filter(BankRecord.payer.contains(u'支付宝')).all()
        # print 'total : %s' % len(items)
        # count = 0
        # for item in items:
        #     order_id, amount = add_order_success(item.id, item.bankcard_name, item.amount)
        #     _LOGGER.info('update_order_status order %s success', order_id)
        #     if order_id:
        #         item.status = RECORD_STATUS.MATCHED
        #         item.order_id = order_id
        #         item.save()
        #         mch_handler.notify_mch(order_id)
        #         redis_cache.del_bank_card_special_amount(item.bankcard_name, amount, item.amount)
        #         _LOGGER.info('update_order_status del_bank_card_special_amount, bankname: %s,  amount: %s, item.amount: %s', item.bankcard_name, amount, item.amount)
        #     print count
        #     count = count + 1
        # print 'end'
        # _LOGGER.info('update_order_status end')
